require "utils"

$drop_property_template_id = 31
$pick_property_template_id = 30
$max_stage = 9999

def min(lhs, rhs)
  return lhs if lhs < rhs
  return rhs
end

def max(lhs, rhs)
  return lhs if lhs > rhs
  return rhs
end

def find_stage_of_drop_item(item)
  stage = $max_stage
  conditions = "item_id=#{item.id} and template_id=#{$drop_property_template_id}"
  properties = ItemProperty.find(:all, :conditions=>conditions)
  return stage if properties.nil?
  return stage if properties.empty?
  properties.each do |property|
    monster = Monster.find(property.reserved1)
    monster.maps.split(",").each do |map_id|
      map_stage = Map.find(map_id).stage
      stage = min(map_stage, stage)
    end
  end
  return stage
end

def find_stage_of_pick_itme(item)
  stage = $max_stage
  conditions = "item_id=#{item.id} and template_id=#{$pick_property_template_id}"
  properties = ItemProperty.find(:all, :conditions=>conditions)
  properties each do |property|
    map_stage = Map.find(property.reserved1)
    stage = min(map_stage, stage)
  end
  return stage
end

def find_stage_of_item(item)
  drop_stage = find_stage_of_drop_item(item)
  pick_stage = find_stage_of_drop_item(item)
  return min(drop_stage, pick_stage)
end

def find_stage_of_alchemy(alchemy)
  stage = 0
  item_stage = find_stage_of_item(Item.find(alchemy.material1))
  stage = max(stage, item_stage)
  return {:stage=>stage, :alchemy=>alchemy} if stage == $max_stage
  
  unless alchemy.material2 == 0
    item_stage = find_stage_of_item(Item.find(alchemy.material2))
    stage = max(stage, item_stage)
    return {:stage=>stage, :alchemy=>alchemy} if stage == $max_stage
  end
  
  unless alchemy.material3 == 0
    item_stage = find_stage_of_item(Item.find(alchemy.material3))
    stage = max(stage, item_stage)
    return {:stage=>stage, :alchemy=>alchemy}
  end
  
  return {:stage=>stage, :alchemy=>alchemy}
end

def show_alchemy(alchemy)
  product = Item.find(alchemy.product)
  material1 = Item.find(alchemy.material1).japaness_name
  str =  "item_type:#{product.item_type} - #{product.japaness_name} = #{material1}*#{alchemy.num1}"
  unless alchemy.material2 == 0
    material2 = Item.find(alchemy.material2).japaness_name
    str = "#{str} + #{material2}*#{alchemy.num2}"
  end
  unless alchemy.material3 == 0
    material3 = Item.find(alchemy.material3).japaness_name
    str = "#{str} + #{material3}*#{alchemy.num3}"
  end
  return str
end

def show_result(results)
  results.each do |result|
    #~ next if result[:stage] == $max_stage
    next if result[:stage] != 7
    alchemy = result[:alchemy]
    #~ puts "stage:#{result[:stage]} - #{show_alchemy(alchemy)}"
    puts "#{show_alchemy(alchemy)}"
  end
end

results = []
alchemies = Alchemy.find(:all)
alchemies.each do |alchemy|
  results << find_stage_of_alchemy(alchemy)
end

#~ results.sort! { |x,y| y[:stage] <=> x[:stage] }
results.sort! do |x,y|
  product_y = Item.find(y[:alchemy].product)
  product_x = Item.find(x[:alchemy].product)
  product_x.item_type <=> product_y.item_type
end

show_result(results)

